package com.siyao.redvip.components.imagepicker.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.siyao.redvip.R;
import com.siyao.redvip.components.base.BaseActivity;
import com.siyao.redvip.components.imagepicker.adapter.ImageGridAdapter;
import com.siyao.redvip.components.imagepicker.adapter.ImageGridAdapter.TextCallback;
import com.siyao.redvip.components.imagepicker.bean.ImageItem;
import com.siyao.redvip.components.imagepicker.utils.AlbumHelper;
import com.siyao.redvip.components.imagepicker.utils.Bimp;
import com.siyao.redvip.utils.Constants;

public class ImageGridActivity extends BaseActivity {
    public static final String EXTRA_IMAGE_LIST = "imagelist";

    private List<ImageItem> dataList;
    private GridView gridView;
    private ImageGridAdapter adapter;
    private AlbumHelper helper;
    private TextView bt;

    private int maxSize = Constants.DEFAULT_IMAGE_MAX_SIZE;

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    Toast.makeText(ImageGridActivity.this,
                            "最多选择" + maxSize + "张图片", 400).show();
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("ImageGridActivity");
        maxSize = getIntent().getIntExtra("maxSize",
                Constants.DEFAULT_IMAGE_MAX_SIZE);
        View appView = getLayoutInflater().inflate(
                R.layout.activity_image_grid, null);
        appMainView.addView(appView, layoutParams);
        setTitleRightButton("取消");
        setTitleText("相册");

        helper = AlbumHelper.getHelper();
        helper.init(getApplicationContext());

        dataList = (List<ImageItem>) getIntent().getSerializableExtra(
                EXTRA_IMAGE_LIST);

        initView();
        bt = (TextView) findViewById(R.id.bt);
        bt.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                ArrayList<String> list = new ArrayList<String>();
                Collection<String> c = adapter.map.values();
                Iterator<String> it = c.iterator();
                for (; it.hasNext(); ) {
                    list.add(it.next());
                }

                for (int i = 0; i < list.size(); i++) {
                    list.set(i, "file://" + list.get(i));
                }
                Intent intentImgs = new Intent();
                intentImgs.setAction("CHOOSEIMG");
                intentImgs.putExtra("path", list);
                ImageGridActivity.this.sendBroadcast(intentImgs);
                ImageGridActivity.this.finish();

                // for (int i = 0; i < list.size(); i++) {
                // if (Bimp.drr.size() < maxSize) {
                // Bimp.drr.add(list.get(i));
                // }
                // }

                System.out.println("Bimp.drr.size()--->" + Bimp.drr.size());
                Bimp.clear();
                finish();
            }

        });

    }

    private void initView() {
        gridView = (GridView) findViewById(R.id.gridview);
        gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        adapter = new ImageGridAdapter(ImageGridActivity.this, dataList,
                maxSize, mHandler);
        gridView.setAdapter(adapter);
        adapter.setTextCallback(new TextCallback() {
            public void onListen(int count) {
                bt.setText("完成" + "(" + count + ")");
            }
        });

        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                adapter.notifyDataSetChanged();
            }

        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.top_right_btn:
                Bimp.clear();
                this.finish();
                break;

            default:
                break;
        }
    }

	@Override
	protected void initComponent() {
		
	}

	@Override
	protected void initData() {
		
	}

	@Override
	protected void setContentView() {
		
	}

	@Override
	protected void beforeInit() {
		
	}
}
